/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module1;

import dao.SanPhamDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.SanPham;

/**
 *
 * @author OSIRIS
 */
public class ThemSanPhamHoaDonDialog extends javax.swing.JDialog {

    /**
     * Creates new form ThemSanPhamHoaDonDialog
     */
    private SanPham info;
    private int soluong;

    public ThemSanPhamHoaDonDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableSanPham = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextSoLuong = new javax.swing.JTextField();
        jBtnAddHoaDon = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jTableSanPham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTableSanPham);

        jLabel1.setText("Số Lượng :");

        jBtnAddHoaDon.setText("Thêm Vào Hóa Đơn");
        jBtnAddHoaDon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAddHoaDonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel1)
                        .addGap(46, 46, 46)
                        .addComponent(jTextSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47)
                        .addComponent(jBtnAddHoaDon))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 652, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnAddHoaDon))
                .addContainerGap(42, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void load() {
        // List<SanPham> ds=SanPhamDAO.getList();
        SanPhamDAO spDao = new SanPhamDAO();
        List ds = spDao.findAll();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("ID");
        dtm.addColumn("Sku");
        dtm.addColumn("Ma Danh Muc");
        dtm.addColumn("Ten San Pham");
        dtm.addColumn("Gia Ban");
        dtm.addColumn("Kick Co");
        dtm.addColumn("Mau Sac");
        dtm.addColumn("So Luong");
        for (int i = 0; i < ds.size(); i++) {
            SanPham dm = (SanPham) ds.get(i);
            Vector vt = new Vector();
            vt.add(dm.getId());
            vt.add(dm.getSku());
            vt.add(dm.getDanhMuc().getId());
            vt.add(dm.getTenSanPham());
            vt.add(dm.getGiaBan());
            vt.add(dm.getKickCo());
            vt.add(dm.getMauSac());
            vt.add(dm.getSoLuong());
            dtm.addRow(vt);
        }
        this.jTableSanPham.setModel(dtm);
    }

    private int getSpIdToTable() {
        int selectedRowIndex = jTableSanPham.getSelectedRow();
        //int selectedColumnIndex = jTable.getSelectedColumn();
        int selectedObject = (Integer) jTableSanPham.getModel().getValueAt(selectedRowIndex, 0);
        return selectedObject;
    }
    private void jBtnAddHoaDonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAddHoaDonActionPerformed
        // TODO add your handling code here:
        try {
            SanPhamDAO spDao = new SanPhamDAO();
            info = spDao.find(getSpIdToTable());
            soluong = Integer.parseInt(jTextSoLuong.getText());
            if (info.getSoLuong() <= soluong) {
                JOptionPane.showMessageDialog(this, "So luong nhieu hon so luong ton");
            } else {
                this.setVisible(false);
            }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "error");
        }

    }//GEN-LAST:event_jBtnAddHoaDonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        load();
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThemSanPhamHoaDonDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThemSanPhamHoaDonDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThemSanPhamHoaDonDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThemSanPhamHoaDonDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ThemSanPhamHoaDonDialog dialog = new ThemSanPhamHoaDonDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAddHoaDon;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableSanPham;
    private javax.swing.JTextField jTextSoLuong;
    // End of variables declaration//GEN-END:variables

    public SanPham getInfo() {
        return info;
    }

    public void setInfo(SanPham info) {
        this.info = info;
    }

    public int getSoluong() {
        return soluong;
    }

    public void setSoluong(int soluong) {
        this.soluong = soluong;
    }
}
